import argparse
import json
import json_tools

parser = argparse.ArgumentParser()

parser.add_argument('--startTime', type=int, default=0)
parser.add_argument('--endTime', type=int, required=True)
parser.add_argument('--oldfile', type=str, required=True)
parser.add_argument('--newfile', type=str, required=True)
parser.add_argument('--uniqueKey', type=str, default="timestamp")

args = parser.parse_args()


#
def filterByUniqueKey(olditem, newitem):
    result1 = False
    uniqueKeys = args.uniqueKey.split(',')
    for uniqueKeyItem in uniqueKeys:
        if olditem[uniqueKeyItem] != newitem[uniqueKeyItem]:
            return result1
    result1 = True
    return result1


oldMsgList = []
with open(args.oldfile, "r") as f1:
    for line in f1.readlines():
        oldMsgTemp = json.loads(line)
        if oldMsgTemp["timestamp"] >= args.startTime and oldMsgTemp["timestamp"] <= args.endTime:
            oldMsgList.append(oldMsgTemp)
newMsgList = []
with open(args.newfile, "r") as f2:
    for line2 in f2.readlines():
        newMsgTemp = json.loads(line2)
        if newMsgTemp["timestamp"] >= args.startTime and newMsgTemp["timestamp"] <= args.endTime:
            newMsgList.append(newMsgTemp)

print("线上数据：%d条, 新链路数据：%d条" % (len(oldMsgList), len(newMsgList)))
# 1、判断多或者少数据
for oldMsgTemp in oldMsgList:
    oldTempFilterResult = list(filter(lambda x: filterByUniqueKey(x, oldMsgTemp), newMsgList))
    if len(oldTempFilterResult) > 0:
        # 2、判断同一条数据时，进行diff
        result = json_tools.diff(oldMsgTemp, oldTempFilterResult[0])
        print("存在匹配个数:", len(oldTempFilterResult))
        print("diff结果：", result)
    else:
        print("线上存在，但新链路不存在：", oldMsgTemp)

# 1、判断多或者少数据
for newMsgTemp in newMsgList:
    newTempFilterResult = list(filter(lambda x: filterByUniqueKey(x, newMsgTemp), oldMsgList))
    if len(newTempFilterResult) > 0:
        # 2、判断同一条数据时，进行diff，上面已经打印过，这里不在处理
        continue
        # result = json_tools.diff(newMsgTemp, newTempFilterResult[0])
        # print("存在匹配个数:", len(newTempFilterResult))
        # print("diff结果：", result)
    else:
        print("线上不存在，但新链路存在：", newMsgTemp)

# 示例：python3 diffkafkamsg_detail.py --startTime 1671438118900 --endTime 1671438119000 --oldfile "./iotTrafficEvent_old.log" --newfile "./iotTrafficEvent_new.log" --uniqueKey "ID,timestamp"